package Homework.Date7_03;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.PseudoColumnUsage;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class StudentDao {
	// 修改
	public int update(Student s) {
		String sql = "update s_student set sname=?,sex=?,telephone=?,gradeid=?,enterdate=?,fromcity=? where stuno=?";
		int rs = 0;
		try (Connection con = DBUtil.getConnection(); PreparedStatement ps = con.prepareStatement(sql);) {
			ps.setString(1, s.getSname());
			ps.setString(2, s.getSex());
			ps.setLong(3, s.getTelephone());
			ps.setLong(4, s.getGradeid());
			ps.setDate(5, s.getEnterdate());
			ps.setString(6, s.getFromcity());
			ps.setLong(7, s.getStuno());
			rs = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return rs;

	}

	// 删除
	public int del(long stuno) {
		String sql = "DELETE FROM s_student WHERE stuno=?";
		int rs = 0;
		try (Connection con = DBUtil.getConnection(); PreparedStatement ps = con.prepareStatement(sql);) {
			ps.setLong(1, stuno);
			rs = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;

	}

	// 增加
	public int add(Student s) {
		String sql = "insert into s_student(sname,sex,telephone,gradeid,enterdate,fromcity) values(?,?,?,?,?,?)";
		int rs = 0;
		try (Connection con = DBUtil.getConnection(); PreparedStatement ps = con.prepareStatement(sql);) {
			ps.setString(1, s.getSname());
			ps.setString(2, s.getSex());
			ps.setLong(3, s.getTelephone());
			ps.setLong(4, s.getGradeid());
			ps.setDate(5, s.getEnterdate());
			ps.setString(6, s.getFromcity());
			rs = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return rs;

	}

	// 查询全部
	public List<Student> search() {
		List<Student> datas = new java.util.ArrayList<>();
		try (Connection con = DBUtil.getConnection();
				PreparedStatement ps = con.prepareStatement("select * from s_student");
				ResultSet rs = ps.executeQuery();) {
			while (rs.next()) {
				datas.add(new Student(rs.getString("sname"), rs.getString("sex"), rs.getLong("telephone"),
						rs.getLong("gradeid"), rs.getDate("enterdate"), rs.getString("fromcity")));
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return datas;

	}

	// 分页查询
	public List<Student> search(int pageNo, int pageSize) {
		int offset = (pageNo - 1) * pageSize;
		List<Student> datas = new java.util.ArrayList<>();

		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = DBUtil.getConnection();
			ps = con.prepareStatement("select * from s_student order by id limit ?,?");
			ps.setInt(1, offset);
			ps.setInt(2, pageSize);
			rs = ps.executeQuery();
			while (rs.next()) {
				datas.add(new Student(rs.getString("sname"), rs.getString("sex"), rs.getLong("telephone"),
						rs.getLong("gradeid"), rs.getDate("enterdate"), rs.getString("fromcity")));
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, ps, con);
		}
		return datas;

	}

	// 查询单个:找不到是null,找到了,返回一个对象
	public Student get(long stuno) {
		Student s = null;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = DBUtil.getConnection();
			ps = con.prepareStatement("select * from s_student where stuno=?");
			ps.setLong(1, stuno);
			rs = ps.executeQuery();
			if (rs.next()) {
				s = new Student(stuno, rs.getString("sname"), rs.getString("sex"), rs.getLong("telephone"),
						rs.getLong("gradeid"), rs.getDate("enterdate"), rs.getString("fromcity"));
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, ps, con);
		}
		return s;

	}
}
